home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / rbsetnv1.zip / UPROMPT.C < prev    next >
Text File  |  1990-08-16  |  2KB  |  100 lines

  1. /*
  2.  * uprompt.c - set the prompt to the current working directory, with
  3.  * forward slashes
  4.  *
  5.  * Author:        R. Brittain                        4/11/90
  6.  *
  7.  * Syntax:
  8.  *        uprompt
  9.  *
  10.  *                    This code placed in the public domain
  11.  *
  12.  */
  13. #include <string.h>
  14. #include <ctype.h>
  15. #include <dos.h>
  16. #include <dir.h>
  17. #include <stdlib.h>
  18. #include <io.h>
  19.  
  20. /* size minimization measures */
  21. setenvp() {}
  22. void exit(int status) { _exit(status); }
  23.  
  24. #define MEMCHECK(x)        if ((x) == NULL) fatal("Out of memory",1)
  25.  
  26. #ifdef UNIXCOMPAT
  27.  #define strupr(x)            (x)        /* disable upper-casing env-vars */
  28. #endif
  29.  
  30. /* prototypes */
  31.  
  32. int      get_env_index(char *var, char **env_var);
  33. int       getenvseg(unsigned *envseg);
  34. unsigned get_env_var(unsigned env_addr, char ***env_var, int *count);
  35. void      put_env_var(unsigned env_addr, unsigned seglen, char **env_var);
  36.  
  37. void      fatal(char *msg, int status);
  38. char      *concat(char *s1, char *s2);
  39. char      *translit(char *s,char c1,char c2);
  40.  
  41. main()
  42. {
  43.     unsigned env_addr, seglen;
  44.     int index, count;
  45.     char dbuf[64], **env_var;
  46.  
  47.     if ( !getenvseg(&env_addr))
  48.         fatal ("Cannot locate environment\n",2);
  49.  
  50.     seglen = get_env_var( env_addr, &env_var, &count );
  51.     index  = get_env_index( "PROMPT", env_var );
  52.  
  53.     /* change the value of the variable */
  54.  
  55.     *(strchr(env_var[index],'=')+1) = '\0';
  56.     env_var[index] = concat(env_var[index],translit(strlwr(getcwd(dbuf,sizeof(dbuf))),'\\','/'));
  57.     env_var[index] = concat(env_var[index]," >");
  58.     put_env_var(env_addr, seglen, env_var);
  59.     return(0);
  60. }
  61.  
  62.  
  63. char *concat(s1, s2)
  64. char *s1, *s2;
  65. {
  66. /*
  67.  * return the concatenation of s1 and s2 in malloced memory
  68.  */
  69.     char *p;
  70.     p = malloc(strlen(s1)+strlen(s2)+2);
  71.     if (p == (char *)NULL) {
  72.         fatal ("\nOut of memory\n",1);
  73.     } else {
  74.         strcpy(p,s1);
  75.         strcat(p,s2);
  76.     }
  77.     return(p);
  78. }
  79.  
  80. void fatal(char *msg, int status)
  81. {
  82.     write(2,msg,strlen(msg));
  83.     exit(status);
  84. }
  85.  
  86. char *translit(s,c1,c2)
  87. char *s, c1, c2;
  88. {
  89. /*  turn all c1 into c2 in string s, in situ. Return pointer to start of s */
  90.     char *p;
  91.     p = s;
  92.     while (*s) {
  93.         if (*s == c1) *s = c2;
  94.         s++;
  95.     }
  96.     return(p);
  97. }
  98.  
  99. #include "envfuncs.c"
  100.